//
//  XGSmallVideoPlayerCell.swift
//  TodayNews
//
//  Created by monkey on 2020/5/21.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import Kingfisher
import BMPlayer

class XGSmallVideoPlayerCell: UITableViewCell, XGRegisterReusableViewProtocol
{
    var smallVideoModel: XGSmallVideoModel? {
        didSet {
            screenNameLabel.text = smallVideoModel?.user?.user_name
            titleLabel.text = smallVideoModel?.caption
            if let url = smallVideoModel?.user?.headurl {
                iconImageView.kf.setImage(with: URL(string: url), options: [.processor(RoundCornerImageProcessor(cornerRadius: 30, targetSize: CGSize(width: 60, height: 60),backgroundColor: UIColor.black))])
            }
        }
    }
        
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
//        guard let scrollView = superview as? UIScrollView else {
//            return super.hitTest(point, with: event)
//        }
//
//        if !isUserInteractionEnabled || isHidden || alpha <= 0.01 {
//            return nil
//        }
//
//        if self.point(inside: point, with: event) {
//            let p = contentView.convert(point, to: scrollView)
//            if scrollView.frame.contains(p) {
//                return scrollView
//            }
//        }
//
//        return self
//    }
    
    // MARK: - 懒加载
    
    /// 用户头像
    private lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 30
        imageView.layer.masksToBounds = true
        return imageView
    }()
    /// 用户昵称
    private lazy var screenNameLabel = UILabel(text: "测试", textColor: .white, fontSize: 15, textAlignment: .left)
    /// 标题
    private lazy var titleLabel = UILabel(text: "测试", textColor: .white, fontSize: 15, textAlignment: .left)
    /// 播放器
    private(set) lazy var playerView: BMPlayer = {
        let player = BMPlayer(customControlView: XGCustomPlayerControlView())
        return player
    }()
}

// MARK: - 私有扩展

private extension XGSmallVideoPlayerCell
{
    func setUpUI() -> Void
    {
        contentView.backgroundColor = UIColor.black
        contentView.addSubview(playerView)
        contentView.addSubview(iconImageView)
        contentView.addSubview(screenNameLabel)
        contentView.addSubview(titleLabel)
                
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalToSuperview().offset(-49)
        }
        iconImageView.snp.makeConstraints { (make) in
            make.bottom.equalTo(titleLabel.snp.top).offset(-10)
            make.left.equalToSuperview().offset(15)
            make.size.equalTo(CGSize(width: 60, height: 60))
        }
        screenNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp.right).offset(15)
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(iconImageView)
        }
        
        playerView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    
        playerView.playStateChanged = { [weak self] (state) -> Void in
            self?.playerView.panGesture.isEnabled = false
        }
    }
}
